package com.momo.mal.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class MalDatabase extends SQLiteOpenHelper {
	public static final String DATABASE = "myanimelist";
	
	public static final String TABLE_ANIME = "anime";
	public static final String TABLE_MANGA = "manga";
	public static final String TABLE_RELATIONS = "relations";
	public static final String TABLE_STATISTIC = "statistic";
	public static final String TABLE_HISTORY = "history";
	
	private static final int VERSION = 6;
	
	public MalDatabase(Context context) {
		super(context, DATABASE, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase sqlitedatabase) {
		sqlitedatabase.execSQL("CREATE TABLE " + TABLE_ANIME + " (" + AnimeColumn.ID + " TEXT PRIMARY KEY, " +
				AnimeColumn.TITLE + " TEXT, " +
				AnimeColumn.ENGLISH + " TEXT, " +
				AnimeColumn.JAPANESE + " TEXT, " +
				AnimeColumn.SYNONYMS + " TEXT, " +
				AnimeColumn.SYSNOPSIS + " TEXT, " +
				AnimeColumn.TYPE + " TEXT, " +
				AnimeColumn.RANK + " TEXT, " +
				AnimeColumn.POPULARITY_RANK + " TEXT, " +
				AnimeColumn.IMAGE_URL + " TEXT, " +
				AnimeColumn.EPISODE + " TEXT, " +
				AnimeColumn.STATUS + " TEXT, " +
				AnimeColumn.START_DATE + " TEXT, " +
				AnimeColumn.END_DATE + " TEXT, " +
				AnimeColumn.GENRE + " TEXT, " +
				AnimeColumn.TAGS + " TEXT, " +
				AnimeColumn.CLASSIFICATION + " TEXT, " +
				AnimeColumn.MEMBER_SCORE + " TEXT, " +
				AnimeColumn.MEMBER_COUNT + " TEXT, " +
				AnimeColumn.FAVORITE_COUNT + " TEXT, " +
				AnimeColumn.PARENT_STORY + " TEXT, " +
				AnimeColumn.LISTED_ANIME_ID + " TEXT, " +
				AnimeColumn.WATCHED_EPISODE + " TEXT, " +
				AnimeColumn.SCORE + " TEXT, " +
				AnimeColumn.WATCHED_STATUS + " TEXT, " +
				AnimeColumn.ADAPTATION + " TEXT, " +
				AnimeColumn.SIDE_STORY + " TEXT, " +
				AnimeColumn.PREQUEL + " TEXT, " +
				AnimeColumn.SEQUEL + " TEXT, " +
				AnimeColumn.ALTERNATIVE + " TEXT, " +
				AnimeColumn.SPIN_OFF + " TEXT, " +
				AnimeColumn.COMMENTS + " TEXT, " +
				AnimeColumn.STATUS_SYNC + " TEXT, " +
				AnimeColumn.PRIORITY + " TEXT)");
		
		sqlitedatabase.execSQL("CREATE TABLE " + TABLE_MANGA + " (" + MangaColumn.ID + " TEXT PRIMARY KEY, " +
				MangaColumn.TITLE + " TEXT, " +
				MangaColumn.RANK + " TEXT, " +
				MangaColumn.IMAGE_URL + " TEXT, " +
				MangaColumn.TYPE + " TEXT, " +
				MangaColumn.STATUS + " TEXT, " +
				MangaColumn.VOLUMES + " TEXT, " +
				MangaColumn.CHAPTERS + " TEXT, " +
				MangaColumn.GENRES + " TEXT, " +
				MangaColumn.MEMBER_SCORE + " TEXT, " +
				MangaColumn.MEMBER_COUNT + " TEXT, " +
				MangaColumn.POPULARITY_RANK + " TEXT, " +
				MangaColumn.FAVORITE_COUNT + " TEXT, " +
				MangaColumn.TAGS + " TEXT, " +
				MangaColumn.SYNOPSIS + " TEXT, " +
				MangaColumn.READ_STATUS + " TEXT, " +
				MangaColumn.LISTED_MANGA + " TEXT, " +
				MangaColumn.CHAPTER_READ + " TEXT, " +
				MangaColumn.VOLUME_READ + " TEXT, " +
				MangaColumn.JAPANESE + " TEXT, " +
				MangaColumn.ENGLISH + " TEXT, " +
				MangaColumn.SYNONYM + " TEXT, " +
				MangaColumn.ANIME_ADAPTATION + " TEXT, " +
				MangaColumn.RELATED_MANGA + " TEXT, " +
				MangaColumn.ALTERNATIVE_VERSION + " TEXT, " +
				MangaColumn.START_DATE + " TEXT, " +
				MangaColumn.END_DATE + " TEXT, " +
				MangaColumn.COMMENTS + " TEXT, " +
				MangaColumn.STATUS_SYNC + " TEXT, " +
				MangaColumn.SCORE + " TEXT, " +
				MangaColumn.PRIORITY + " TEXT)");
		
		sqlitedatabase.execSQL("CREATE TABLE " + TABLE_STATISTIC + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
				"anime_days TEXT, " +
				"manga_days TEXT )");
		
		sqlitedatabase.execSQL("CREATE TABLE " + TABLE_HISTORY + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
				HistoryColumn.TITLE + " TEXT, " +
				HistoryColumn.DESCRIPTION + " TEXT, " +
				HistoryColumn.UPDATE_DATE + " TEXT, " +
				HistoryColumn.TYPE + " TEXT, " +
				HistoryColumn.ANIME_ID + " TEXT, " +
				HistoryColumn.MANGA_ID + " TEXT)"); 
		
		sqlitedatabase.execSQL("CREATE TABLE " + TABLE_RELATIONS + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
				RelatedColumn.TITLE + " TEXT, " +
				RelatedColumn.TYPE + " TEXT, " +
				RelatedColumn.RELATED_ID + " TEXT, " +
				RelatedColumn.MASTER_ID + " TEXT, " +
				RelatedColumn.IS_ANIME + " TEXT)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase sqlitedatabase, int oldVersion, int newVersion) {
		if(newVersion > oldVersion) {
			sqlitedatabase.execSQL("ALTER TABLE " + TABLE_ANIME + " ADD COLUMN " + AnimeColumn.PRIORITY + " TEXT DEFAULT '0'");
			sqlitedatabase.execSQL("ALTER TABLE " + TABLE_MANGA + " ADD COLUMN " + MangaColumn.PRIORITY + " TEXT DEFAULT '0'");
		}
	}
}
